package com.zjitc.backend_shop.security;

import com.alibaba.druid.support.json.JSONUtils;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.zjitc.backend_shop.utils.APIResponse;
import jakarta.servlet.ServletException;
import jakarta.servlet.ServletOutputStream;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.authentication.AuthenticationFailureHandler;
import org.springframework.stereotype.Component;

import java.io.IOException;

/*
    @author： 尤安托
    @date： 2025/4/12 15:04
*/
@Component
public class LoginFailureHandler implements AuthenticationFailureHandler {
    private final ObjectMapper objectMapper = new ObjectMapper();
    @Override
    public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response, AuthenticationException exception) throws IOException, ServletException {
        APIResponse apiResponse = new APIResponse();
        response.setContentType("application/json;charset=UTF-8");
        String message= exception.getMessage();
        apiResponse.APIResponseError(1000, message, null);
        response.getWriter().write(objectMapper.writeValueAsString(apiResponse));
    }
}
